# This Python file uses the following encoding: utf-8

import config
import func
import tushare as ts
import time
from catchObj.Stocks import Stocks


class TuShare:

    def __init__(self):
        self.api_key = config.api_key
        pass

    #从tushare获取所有的股票
    #['000009.SZ', '20010406', 7.8, 7.88, 7.69, 7.75, 7.77, -0.02, -0.26, 63153.99, 48979.9756]
    #代码,日期,开盘,高,低,收,昨收,涨跌额,涨跌幅,成交量(手),成交额(千元)
    def updateAllStocks(self):
        pro = ts.pro_api(config.api_key)
        data = pro.stock_basic(exchange='', list_status='L', fields='ts_code,symbol,name,area,industry,list_date')
        objJsonData = func.to_json2(data)
        result = Stocks.collections.allstockname.insert_one({'data':objJsonData['data'],'getdate':time.time()})

    #从tushare 获取所有的日线数据
    #code,000009.SZ
    def updateAllDaily(self, code):
        pro = ts.pro_api(config.api_key)
        df = pro.daily(ts_code=code, start_date='20000101', end_date=time.strftime("%Y%m%d", time.localtime()))
        objJsonData = func.to_json2(df)
        # collections.daily.insert_one({'code':stock[0],'data':objJsonData['data'],'getdate':time.time()})
        Stocks.collections.daily.update_one(
            {'code':code},
            {"$set": {"data": objJsonData['data'],'getdate':time.time()}},
            upsert=True
        )
